// $Id: xstandard.js,v 1.1.2.1 2009/03/12 02:21:50 henrrrik Exp $

function xstandard_sync() {
  // For every registered XStandard element, operate on it
  for (var i in Drupal.settings.xstandard) {
    var id = Drupal.settings.xstandard[i].id;

    // Make sure this element still exists
    if ($('#' + id + '-editor').length > 0) {
      $('#' + id + '-editor').EscapeUnicode = true;

      // jQuery will always return a string when we use .val(), so use the old-fashioned way
      var value = document.getElementById(id + '-editor').value;
      if (typeof(value) != 'undefined') {
        // Sync back to both the original input element and the editor's object
        $('#' + id).val(value.replace(/<!-- break -->/i, '<!--break-->'));
        $('#' +id + '-editor param[name=Value]').val($('#' + id).val());
	  }
    }
  }

  // Just in case something is added to the settings, we should keep this timeout running
  var timeout = Drupal.settings.xstandard.length > 0 ? 50 : 1000;
  setTimeout(function () { xstandard_sync(); }, timeout);
}

// Only need to do this once, otherwise this would be placed in Drupal.behaviors
$(function() {
  xstandard_sync();
});
